
/**
* Copyright (C) 2007 Pedro Miller Rabinovitch.
* http://www.opensource.org/licenses/mit-license.php
*
* Basically, feel free to use it however you want to.
* Please give me credit where appropriate.
*
* Pedro "Shade" Miller, gmail user: pedromr
* http://exodia.org
*
* @author Miller
*
* $Id: TestWorld.as 8 2007-12-13 10:26:42Z pedromr $
*/
package org.exodia.seeds {
	import flash.display.MovieClip;
	import flash.events.Event;
	import org.exodia.seeds.ppv.AgentPPV;
	import org.exodia.seeds.ppv.WorldPPV;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.cameras.FreeCamera3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.Papervision3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	//import fl.controls.Button;
	//import fl.events.ComponentEvent;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import org.exodia.seeds.sprites.WorldShape;
	import org.exodia.seeds.model.World;
	import org.exodia.seeds.model.Agent;
	import org.exodia.seeds.sprites.AgentShape;
	import org.exodia.seeds.behaviours.*;
	import org.exodia.math.Vector3D;
	
	/**
	* ...
	* @author Default
	*/
	public class Test3D extends MovieClip {
		var _renderer:BasicRenderEngine;
		var _viewport:Viewport3D;
		var _scene:Scene3D;
		var _camera:Camera3D;
		
		public function Test3D() {
			initScene();

		
			_world = new World();
			var margin:Number = 20;
			var panelWidth:Number = 150;
			//_world.width = stage.stageWidth - 3*margin - panelWidth;
			//_world.height = stage.stageHeight - 2 * margin;
			//_world.depth = stage.stageHeight;
			_world.width = 1000;
			_world.height = 600;
			_world.depth = 600;
			_worldPPV = new WorldPPV(_world);
						
			/*
			_worldShape.x = panelWidth+2*margin;
			_worldShape.y = margin;
			*/
			_scene.addChild( _worldPPV );
			_camera.target = _worldPPV;
			
			
			var lastUIy:Number = margin;
			/*
			var btnPause:Button = new Button();
			btnPause.width = panelWidth;
			btnPause.label = "Pause";
			btnPause.addEventListener( ComponentEvent.BUTTON_DOWN, function( evt:ComponentEvent) {
				if( _timer.running ) {
					_timer.stop()
					btnPause.label = "Resume";
				} else {
					_timeLastTick = 0;
					_timer.start()
					btnPause.label = "Pause";
				}
			});
			btnPause.x = margin;
			btnPause.y = lastUIy;
			addChild( btnPause );
			lastUIy = btnPause.y + btnPause.height;
			*/
			
/*
			targetMark = new Sprite();
			var g:Graphics = targetMark.graphics;
			g.lineStyle( 1, 0 );
			var r:Number = 5;
			var l:Number = r * 1.5;
			g.drawCircle( 0.5, 0.5, r );
			g.moveTo( -l,  0 );
			g.lineTo(  l,  0 );
			g.moveTo(  0, -l );
			g.lineTo(  0,  l );
			_worldShape.addChild( targetMark );
			
			//_worldShape.visible = false;
			
			_worldShape.addEventListener( MouseEvent.CLICK, handleClick );
			_worldShape.addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove );
*/
			_randWalkBehaviour = new RandomWalkBehaviour( 0.1, 4000 );
			_separationBehaviour = new SeparationBehaviour( 1.5 );
			_followBehaviour = new SeekBehaviour( 1 );
			_targetedBehaviour = new SeekBehaviour( 2 );
			
			var range:Number = 150;
			var nAgents:uint = 20;
			
			for( var i:int = 0; i < nAgents; i++ ) {
				var agent:Agent = new Agent( _world );
				var agentPPV:AgentPPV = new AgentPPV( agent );
				
				agent.radius = 30;
				agent.maxSpeed = 0.12;
				agent.maxForce = 0.0003;
				agent.sensorRangeSquared = range*range;
				
				agent.addBehaviour( _randWalkBehaviour );
				agent.addBehaviour( _separationBehaviour );
				/**
				if ( i == 0 ) {
					_leader = agent;
					agent.radius = 10;
					agent.addBehaviour( _targetedBehaviour );
					//agentShape.rgbFillColor = 0xC5FEE2;
				} else {
					if ( i == 1 )
						agent.radius = 20;
					agent.addBehaviour( _followBehaviour );
				}
				/**/
				
				agent.position.x = (Math.random()*0.8 - 0.4) * _world.width;
				agent.position.y = (Math.random()*0.8 - 0.4) * _world.height;
				agent.position.z = (Math.random()*0.8 - 0.4) * _world.depth;
				_worldPPV.addAgent( agentPPV );
				
				//trace( "agent at " + agent.position );
				
				//agentShape.addEventListener( MouseEvent.CLICK, handleAgentClicked );
				//agentShape.buttonMode = true;
				
			}
						
			
			_timeLastTick = getTimer();
			_timer = new Timer( 1000 / 30 );
			_timer.addEventListener( TimerEvent.TIMER, handleTimerTick );
			_timer.start();
		}
		
		
		function initScene() {
			Papervision3D.useDEGREES = false;
			_viewport = new Viewport3D(0, 0, true, false);
			addChild(_viewport);
			_renderer = new BasicRenderEngine();
			_scene = new Scene3D();
			_camera = new Camera3D(null, 6);

			//_scene.addChild( new AgentPPV( null ));
			//_scene.addChild( new Sphere( new ColorMaterial()));

		}
		
		var _world:World;
		var _worldPPV:WorldPPV;
		
		var _timer:Timer;
		var _timeLastTick:uint;
		
		var _followBehaviour:SeekBehaviour;
		var _targetedBehaviour:SeekBehaviour;
		var _randWalkBehaviour:RandomWalkBehaviour;
		var _separationBehaviour:SeparationBehaviour;
		
		var targetMark:Sprite;
		var _leader:Agent;
		
		function handleTimerTick( evt:TimerEvent ) {
			var now:uint = getTimer();
			var deltaMS:uint = now - _timeLastTick;
			
			if( _timeLastTick != 0 ) {
				if( _leader )
					_followBehaviour.setTarget( _leader.position );
				
				_worldPPV.tick( deltaMS );
				
				var k:Number = 5;
				_camera.x = k*(mouseX-width/2);
				_camera.y = k*(mouseY-height/2);
				
				//trace( _leader.position, _leader.speed );
				_renderer.renderScene( _scene, _camera, _viewport );
			}
			
			_timeLastTick = now;
		}
	}
	
}
